package com.fjrcloud.service;

import com.fjrcloud.dao.UserDao;
import com.fjrcloud.entity.ProjectGroup;
import com.fjrcloud.entity.User;
import com.fjrcloud.entity.UserType;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.utils.ConfigInfo;
import com.fjrcloud.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by shengjian.lin on 17-2-21.
 */
@Service
public class UserService {

    @Autowired
    private ConfigInfo configInfo;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ProjectService service;

    public User get(Integer id){
        return userDao.findOne(id);
    }
    public User get(String userName){
        return userDao.findByUserName(userName);
    }

    public void del(Integer id){
        userDao.delete(id);
    }

    public User save(User user,Integer groupId) throws BusinessException {

        if (user.getId() == null){
            User _user = userDao.findByUserName(user.getUserName());
            if (_user != null)
                throw new BusinessException("用户名已经存在");

            if(configInfo.getUsername().equals(user.getUserName()))
                throw new BusinessException("用户名已经存在");


            if (groupId == null){
                throw new BusinessException("项目组不能为空");
            }
            ProjectGroup group = service.get(groupId);
            if (group == null)
                throw new BusinessException("找不到项目组");

            user.setGroup(group);

        }else{
            User old = userDao.findOne(user.getId());
            if (old == null)
                throw new BusinessException("更新对象无效");

            user.setUserName(old.getUserName());
            user.setGroup(old.getGroup());

            if (user.getPassword() == null){
                user.setPassword(old.getPassword());
            }
        }


        return userDao.save(user);
    }
    public User change(Integer id,String old,String password) throws BusinessException {

        if (id == null)
            throw new BusinessException("对象错误");
        if (StringUtils.isEmpty(old))
            throw new BusinessException("旧密码错误");
        if (StringUtils.isEmpty(password))
            throw new BusinessException("新秘密不能为空");

        User user = userDao.findOne(id);

        if (user == null)
            throw new BusinessException("对象错误");

        if (!user.getPassword().equals(old))
            throw new BusinessException("旧密码错误");

        user.setPassword(password);

        return userDao.save(user);
    }

    public UserType loginVerify(String username, String password) throws BusinessException {
        if (username == null)
            throw new BusinessException("请输入用户名");
        if (password == null)
            throw new BusinessException("请输入密码");

        User user = userDao.findByUserNameAndPassword(username,password);

        if (user == null){
            if(username.equals(configInfo.getUsername()) && password.equals(configInfo.getPassword()))
                return UserType.root;
            else
                throw new BusinessException("用户名或密码错误");
        }else{
            return user.getType();
        }
    }

    private PageRequest getPageRequest(Integer pageNum, Integer pageSize){
        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        return new PageRequest(pageNum, pageSize);
    }

    public Page<User> list(Integer pageNum, Integer pageSize){
        return userDao.findAll(getPageRequest(pageNum,pageSize));
    }
}
